package com.caojm.mptools.commands;

import com.caojm.mptools.Mptools;
import com.caojm.mptools.utils.DatabaseHelper;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;

public class UnbindCommand {
    public static void register(){
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
                dispatcher.register(
                        CommandManager.literal("mp")
                                .then(CommandManager.literal("unbind")
                                        .executes(context -> {
                                            ServerCommandSource source = context.getSource(); 
                                            String playerName = source.getName(); 

                                            boolean success = DatabaseHelper.unbindAccount(playerName);

                                            if (success) {
                                                source.sendFeedback(() -> Text.of("账号解绑成功！"), false);
                                                Mptools.LOGGER.info("玩家 " + playerName + " 解绑了账号");
                                            } else {
                                                source.sendFeedback(() -> Text.of("账号解绑失败，请稍后再试。"), false);
                                                Mptools.LOGGER.error("玩家 " + playerName + " 解绑账号失败");
                                            }
                                            return 1;
                                        })
                                )
                )
        );
    }
}
